package webapp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.PreparedStatement;

/**
 * Servlet implementation class Delete
 */
public class Delete extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private ServletConfig config;
	

    

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		this.config=config;
	}

	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		System.out.println("We are in Service method of Delete");
		PrintWriter out = response.getWriter();
		
		String todelete=request.getParameter("deleteid");
		
		String msg=" ";
		// Connection conn = null;
				 String url = "jdbc:mysql://localhost:3306/";
				 String dbName = "webappschema";
				 String driver = "com.mysql.jdbc.Driver";
				 String dbusername = "root";
				 String dbpassword = "webapp";
				 List dataList = new ArrayList(); 
				 
				 try {Class.forName(driver).newInstance();
				 Connection conn = DriverManager.getConnection(url+dbName,dbusername,dbpassword);
				 String strQuery = ("Delete from Purchase where Purchaseid="+ todelete );
				 Statement st = conn.createStatement();
				 int rs = st.executeUpdate(strQuery);
				 response.setContentType("text/html");
				 
				 //while(rs.next()){
					 
					 //dataList.add(rs.getInt("Purchaseid"));
					 //dataList.add(rs.getInt("Grossweight"));
					 //dataList.add(rs.getInt("Taraweight"));
					 //dataList.add(rs.getInt("Netweight"));
					 //dataList.add(rs.getInt("Date"));
					 //dataList.add(rs.getString("AgencyName"));
					 //dataList.add(rs.getString("RiceMillName"));
					 
				 //}
				 
				 //rs.close();
				 st.close();
			 	 } catch (Exception e) {
			 	   e.printStackTrace();
			 	 }
	    	 request.setAttribute("data", dataList);
	    	 RequestDispatcher dispatcher = request.getRequestDispatcher("ViewEntryPurchase.jsp");

	    	  if (dispatcher != null){

	    	  dispatcher.forward(request, response);
	    	//	msg = "Hi"  ;

	    	  }	    
}


}
